In [1]:
# In this Python script we attempt to develop a scheme to import and then
# process data from multiple files.
import numpy as np 
In [2]:
# Before importing multiple files, we first have to learn about some of the
# properties of the '%' operator.  The command '%f %n' outputs the floating
# point number n.
print("%f" %3.21)
3.210000
In [3]:
# If we put a number m in front of the f, it specifies that the number
# output should contain m characters.  In the example below, there's lots
# of white space in front of the output.
print("%24f" %3.21)
                3.210000
In [4]:
# We can force the white space to be filled with zeros by putting a 0
# (zero) in front of the number m.
print("%024f" %3.21)
00000000000000003.210000
In [5]:
# We can also specify the number of characters that should follow the
# decimal point using .p before the f where p is a postive integer.
print("%024.6f" %3.21)
print("%024.6f" %321)
00000000000000003.210000
00000000000000321.000000
In [6]:
# Suppose our files a labelled something like file001data.txt,
# file002data.txt, ...  We can use %f inside a loop to make the 001,
# 002, ...  We want our numbers to be m = 3 characters long and to have p =
# 0 characters after the decimal place.
for i in range(3):
    print("%03.0f" %i)
000
001
002
In [7]:
# We combine our %f statements with additional text to create the full file
# name.
for i in range(3):
    print("file%03.0fdata.txt" %i)
file000data.txt
file001data.txt
file002data.txt
In [8]:
# The following acheives exactly the same output but, in my opinion, is a little
# cleaner.
head = "file"
toe = "data.txt"
for i in range(3):
    print(head + "%03.0f" %i + toe)
file000data.txt
file001data.txt
file002data.txt
In [9]:
# We now actually load the contents of 5 sequential data files into separate arrays.
dataArray = []
for i in range(1, 6, 1):
    dataArray = dataArray + [np.loadtxt(head + "%03.0f" %i + toe)]
In [11]:
# We can examine the contents of each array.
for i in range(5):
    print('The contents of file ', i + 1, ': ', dataArray[i], sep = '')
The contents of file 1: [1.  1.1 1.2 1.3 1.4]
The contents of file 2: [2.  2.1 2.2 2.3 2.4]
The contents of file 3: [3.  3.1 3.2 3.3 3.4]
The contents of file 4: [4.  4.1 4.2 4.3 4.4]
The contents of file 5: [5.  5.1 5.2 5.3 5.4]
In [13]:
# Maybe we only want every second file to be imported.  Only a slight
# change to the for loop control statement is required.    
oddData = []
for i in range(1, 6, 2):
    oddData = oddData + [np.loadtxt(head + "%03.0f" %i + toe)]
Out[13]:
[array([1. , 1.1, 1.2, 1.3, 1.4]),
 array([3. , 3.1, 3.2, 3.3, 3.4]),
 array([5. , 5.1, 5.2, 5.3, 5.4])]
In [14]:
# We can examine the contents of each odd numbered array.
for i in range(3):
    print('The contents of every odd file ', 2*i + 1, ': ', oddData[i], sep = '')
The contents of every odd file 1: [1.  1.1 1.2 1.3 1.4]
The contents of every odd file 3: [3.  3.1 3.2 3.3 3.4]
The contents of every odd file 5: [5.  5.1 5.2 5.3 5.4]
In [15]:
# Here we import only the even files...
evenData = []
for i in range(2, 6, 2):
    evenData = evenData + [np.loadtxt(head + "%03.0f" %i + toe)]
In [16]:
# ... and then examine their contents.
for i in range(2):
    print('The contents of every even file ', 2*(i + 1), ': ', evenData[i], sep = '')
The contents of every even file 2: [2.  2.1 2.2 2.3 2.4]
The contents of every even file 4: [4.  4.1 4.2 4.3 4.4]
In [17]:
# If you want to select only specific files to be imported using
# non-sequential numbers, you can do the following.
selectedData = []
importList = [1, 2, 3, 4, 5, 8, 12]
for i in range(len(importList)):
    selectedData = selectedData + [np.loadtxt(head + "%03.0f" %importList[i] + toe)]
In [18]:
# The contents of the selected files.
for i in range(len(importList)):
    print('The contents of each selected file %02.0f' %importList[i], ': ', selectedData[i], sep = '')
The contents of each selected file 01: [1.  1.1 1.2 1.3 1.4]
The contents of each selected file 02: [2.  2.1 2.2 2.3 2.4]
The contents of each selected file 03: [3.  3.1 3.2 3.3 3.4]
The contents of each selected file 04: [4.  4.1 4.2 4.3 4.4]
The contents of each selected file 05: [5.  5.1 5.2 5.3 5.4]
The contents of each selected file 08: [8.  8.1 8.2 8.3 8.4]
The contents of each selected file 12: [12.  12.1 12.2 12.3 12.4]
In [20]:
# Now we can do calculations using the imported data.  For example, the
# element-by-element average of the 7 imported files is:
elementAvg = []
for j in range(len(selectedData[0])):
    tot = 0
    for i in range(len(importList)):
        tot += selectedData[i][j]
    elementAvg += [tot/len(importList)]
elementAvg
Out[20]:
[5.0, 5.1000000000000005, 5.2, 5.3, 5.3999999999999995]
In [21]:
# Here's the average of each individual file:
fileAvg = []
for i in range(len(importList)):
    tot = 0;
    for j in range(len(selectedData[0])):
        tot += selectedData[i][j]
    fileAvg += [tot/len(selectedData[0])]
fileAvg
Out[21]:
[1.2, 2.2, 3.2, 4.2, 5.2, 8.2, 12.2]